package com.example.nick.bodyapplication.recycler

import android.graphics.SurfaceTexture
import android.util.Log
import android.view.LayoutInflater
import android.view.TextureView
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.compose.ui.graphics.Color
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.example.nick.bodyapplication.R

/**
 * Created on 2021/7/30.
 * @author Holo
 */
class GridAdapter : BaseQuickAdapter<Int, BaseViewHolder>(R.layout.list_item, mutableListOf()) {

    init {
        addChildClickViewIds(R.id.itemIvPlay)
    }

    override fun createBaseViewHolder(view: View): BaseViewHolder {
        val count = if (recyclerView.layoutManager is GridLayoutManager) (recyclerView.layoutManager as GridLayoutManager).spanCount else 1
        view.layoutParams.height = recyclerView.height / count
        return super.createBaseViewHolder(view)
    }

//    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder {
//        return super.onCreateViewHolder(parent, viewType)
//        // 重新加载导致点击事件监听不到
//        var view = LayoutInflater.from(context).inflate(R.layout.list_item, parent, false)
//        view.layoutParams.height = parent.height / 3
//        var holder = BaseViewHolder(view)
//        return holder
//    }

    override fun convert(holder: BaseViewHolder, item: Int) {
        holder.setText(R.id.tvShow, "item:$item")
        holder.setBackgroundColor(R.id.itemRoot, getItemBgColor(getItemPosition(item)))
        val itemPlayView = holder.getView<TextureView>(R.id.itemPlayView)
        itemPlayView.surfaceTextureListener = object : TextureView.SurfaceTextureListener {
            override fun onSurfaceTextureAvailable(
                surface: SurfaceTexture,
                width: Int,
                height: Int
            ) {
                Log.i("list", "onSurfaceTextureAvailable item:$item")
            }

            override fun onSurfaceTextureSizeChanged(
                surface: SurfaceTexture,
                width: Int,
                height: Int
            ) {
            }

            override fun onSurfaceTextureDestroyed(surface: SurfaceTexture): Boolean {
                Log.i("list", "onSurfaceTextureDestroyed item:$item")
                return false
            }

            override fun onSurfaceTextureUpdated(surface: SurfaceTexture) {
            }

        }
        val itemIvPlay = holder.getView<ImageView>(R.id.itemIvPlay)
        itemIvPlay.setOnClickListener {
            Log.i("list", "itemIvPlay click item:$item")
            notifyItemChanged(item)
        }
//        holder.setBackgroundColor(R.id.tvShow, getBgColor(getItemPosition(item)))
    }

    fun getBgColor(pos: Int): Int {
        when(pos % 4) {
            0 -> return context.getColor(R.color.purple_200)
            1 -> return context.getColor(R.color.purple_500)
            2 -> return context.getColor(R.color.teal_200)
            else -> return context.getColor(R.color.purple_700)
        }
    }

    fun getItemBgColor(pos: Int): Int {
        when(pos % 4) {
            1 -> return context.getColor(R.color.purple_200)
            2 -> return context.getColor(R.color.purple_500)
            3 -> return context.getColor(R.color.teal_200)
            else -> return context.getColor(R.color.purple_700)
        }
    }
}